package com.lcq.gulimall.search.service.impl;

import com.alibaba.fastjson.JSON;
import com.lcq.common.to.SkuSearchInfoTo;
import com.lcq.gulimall.search.config.ElasticSearchConfig;
import com.lcq.gulimall.search.constant.ElasticConstant;
import com.lcq.gulimall.search.service.ProductService;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service("productService")
public class ProductServiceImpl implements ProductService {
    @Autowired
    RestHighLevelClient restHighLevelClient;

    @Override
    public void up(List<SkuSearchInfoTo> skuSearchInfoTos) throws IOException {
        if(skuSearchInfoTos==null||skuSearchInfoTos.size()==0){
            return;
        }
        BulkRequest requests = new BulkRequest();

        skuSearchInfoTos.stream().forEach(to->{
            IndexRequest request = new IndexRequest(ElasticConstant.PRODUCT_INDEX);
            request.id(to.getSkuId().toString());
            request.source(JSON.toJSONString(to), XContentType.JSON);
            requests.add(request);
        });


        requests.add();
        restHighLevelClient.bulk(requests, ElasticSearchConfig.COMMON_OPTIONS);
    }
}
